﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Shopping.Common;
using Shopping.Model;
using Shopping.BLL;

public partial class Home_ConfirmOrder : BaseUserPage
{
    protected ShoppingCartsManage bllCarts = new ShoppingCartsManage();
    protected UserAddressManage bllAddress = new UserAddressManage();
    protected CouponsManage bllCoupon = new CouponsManage();
    protected PayModesManage bllPay = new PayModesManage();
    protected DeliveryManage bllSend = new DeliveryManage();
   

    protected List<ShoppingCartInfo> listOfCarts;
    protected List<UserAddressInfo> listOfAddress;
    protected List<Coupons> listOfCoupon; //优惠券信息；
    protected List<PayModes> listOfPayMode;
    protected List<Deliveries> listOfSendMode;
    protected string SelectOptionOfProvince; //省份选择选项；
    protected string SelectOptionOfCity; //城市下拉选项；
    protected string SelectOptionOfArea; //区域下拉选项；
    protected UserAddressInfo curAddress;
    protected OrderInfo order;

    protected decimal totalGoodsMoney;
    protected decimal sendFee = 0.00M;
    protected int totalWeight;
  

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //加载购物车中的数据；
            listOfCarts = bllCarts.QueryList(bllCarts.GetShoppingCartCookieName(this.UserID));
            listOfAddress = bllAddress.QuerList(this.UserID, 1);
            listOfCoupon = bllCoupon.GetActiveCouponsByUserId(this.UserID);
            listOfPayMode = bllPay.QueryALL(true);
            listOfSendMode = bllSend.Query(true);

            if (listOfAddress == null) listOfAddress = new List<UserAddressInfo>();
            if (listOfPayMode == null) listOfPayMode = new List<PayModes>();
            if (listOfSendMode == null) listOfSendMode = new List<Deliveries>();

            if (listOfAddress.Count > 0)
                curAddress = listOfAddress[0];
            else
                curAddress = new UserAddressInfo();

            SelectOptionOfProvince = ProvinceCityAreasManage.QueryProvincesToSelectOption(curAddress.Area.Province.ID.ToString());
            SelectOptionOfCity = ProvinceCityAreasManage.QueryCitiesToSelectOption(curAddress.Area.Province.ID, curAddress.Area.Area.CityID.ToString());
            SelectOptionOfArea = ProvinceCityAreasManage.QueryAreasToSelectOption(curAddress.Area.Area.CityID, curAddress.AddressInfo.AreaID.ToString());

            order = new OrderInfo();
            order.Order.OrderDate = DateTime.Now;
            if (listOfCarts != null)
            {
                foreach (ShoppingCartInfo c in listOfCarts)
                {
                    OrderLineInfo ol = new OrderLineInfo();
                    ol.LineInfo.Discount = c.CartInfo.Discount;
                    ol.LineInfo.GoodsID = c.CartInfo.GoodsID;
                    ol.LineInfo.PackUnitID = c.CartInfo.PackUnitID;
                    ol.LineInfo.PresentScores = c.CartInfo.PresentScores;
                    ol.LineInfo.Price = c.CartInfo.Price;
                    ol.LineInfo.Quantity = c.CartInfo.Quantity;
                    order.Lines.Add(ol);
                    totalWeight += c.Weight * c.CartInfo.Quantity;
                    totalGoodsMoney += c.CartInfo.Quantity * c.CartInfo.Price;
                }
            }

            if (curAddress != null && listOfSendMode != null)
            {
                sendFee = bllSend.ComputeSendFee(listOfSendMode[0].DeliveryID, curAddress.AddressInfo.AreaID, totalWeight);
            }


        }
    }
}